package com.estar.assistsurvy.activity;

import java.util.ArrayList;

import com.estar.assistsurvy.R;
import com.estar.assistsurvy.adapter.CetificationAdapter;
import com.estar.assistsurvy.entity.CetificationItem;
import com.estar.assistsurvy.entity.EsSignedInfo;
import com.estar.assistsurvy.entity.RequestCertifiedStatus;
import com.estar.assistsurvy.entity.ResponseCertifiedStatus;
import com.estar.assistsurvy.thread.CertifiedStatusThread;
import com.estar.assistsurvy.utils.MightypleUtil;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class CetificationActivity extends BaseActivity implements OnClickListener,OnItemClickListener{

	private TextView returnHome;
	private ListView cetifi_list;
	private int[] icons=new int[]{R.drawable.certification01,R.drawable.certification02,R.drawable.certification03};
	private String[] names=new String[]{"基本信息","资质照片","在线答题"};

	private ArrayList<CetificationItem> list;
	private CetificationAdapter adapter;
	private Intent intent;
	private String uuid,deviceId;
	private SharedPreferences preference;
	private String[] status;
	private ProgressDialog dialog;
	private Handler handler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case -1:
				showShortToast((String) msg.obj);
				break;

			case 0:
				dialog.dismiss();
				ResponseCertifiedStatus certified=new ResponseCertifiedStatus();
				certified=(ResponseCertifiedStatus) msg.obj;
				showShortToast(certified.getMessages());
				EsSignedInfo esSignedInfo=new EsSignedInfo();
				esSignedInfo=certified.getEsSignedInfo();
				if(esSignedInfo!=null&&esSignedInfo.getBaseState()!=null&&!esSignedInfo.getBaseState().equals("")){
					status=new String[]{esSignedInfo.getBaseState(),esSignedInfo.getPhotoState(),esSignedInfo.getAnswerState()};
					adapter.setStatus(status);
					adapter.notifyDataSetChanged();
				}else{
					showShortToast("网络不给力!");
				}
				
				break;

			default:
				break;
			}
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cetification);
		initView();
	}

	private void initView(){
		returnHome=find(R.id.returnBack);
		returnHome.setOnClickListener(this);
		cetifi_list=find(R.id.cetifi_list);
		getList();
		adapter=new CetificationAdapter(this, list);
		cetifi_list.setAdapter(adapter);
		cetifi_list.setOnItemClickListener(this);


		preference=getSharedPreferences("user", MODE_PRIVATE);
		uuid=preference.getString("uuid", "");
		deviceId=MightypleUtil.getDeviceId(CetificationActivity.this);
		RequestCertifiedStatus CertifiedStatus=new RequestCertifiedStatus(deviceId, uuid);
		dialog=ProgressDialog.show(this, null, "状态获取中…", false, false);
		new CertifiedStatusThread(CetificationActivity.this,CertifiedStatus,  handler).start();
	}

	private void getList(){
		list=new ArrayList<>();
		for(int i=0;i<icons.length;i++){
			CetificationItem item=new CetificationItem(icons[i], names[i]);
			list.add(item);
		}

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		finish();
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		if(status!=null){
//			if(status[position].equals("0")||status[position].equals("3")){

				switch (position) {
				case 0:
					intent=new Intent(CetificationActivity.this, BaseInfoActivity.class);
					startActivity(intent);
					finish();
					break;

				case 1:
					intent=new Intent(CetificationActivity.this, QualificationActivity.class);
					startActivity(intent);
					finish();
					break;

				case 2:
					intent=new Intent(CetificationActivity.this, ExamOnlineActivity.class);
					startActivity(intent);
					finish();
					break;

				default:
					break;
				}	
				
//			}else if(status[position].equals("1")){
//				showShortToast("审核中，请耐心等待");
//			}else if(status[position].equals("2")){
//				showShortToast("审核已通过");
//			}
		}else{
			showShortToast("认证状态获取失败，请刷新重试！");
		}
	}

}
